package com.joysuch.wwyt.bp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.bp.bean.BpHiddenDangerBean;
import com.joysuch.wwyt.bp.bean.BpTaskInvestigateBean;
import com.joysuch.wwyt.bp.bean.investigate.TaskDelBean;
import com.joysuch.wwyt.bp.bean.investigate.TaskDelDTO;
import com.joysuch.wwyt.bp.bean.investigate.TaskSearchBean;
import com.joysuch.wwyt.bp.bean.investigate.TaskSearchVo;
import com.joysuch.wwyt.bp.bean.investigate.vo.TaskListVo;
import com.joysuch.wwyt.bp.bean.investigate.vo.TaskVo;
import com.joysuch.wwyt.bp.bean.vo.TaskStatiUserVo;
import com.joysuch.wwyt.bp.entity.vo.CheckRecordVo;
import com.joysuch.wwyt.bp.service.BpInvestigateTaskService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.quartz.createtable.constant.TableNamePrefixConstant;
import com.joysuch.wwyt.quartz.createtable.handler.CreateTableHandler;
import com.joysuch.wwyt.repeatsubmit.NoRepeatSubmit;
import com.joysuch.wwyt.util.StringUtil;
import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author heqifeng
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/bp/bp-invest-task")
@Slf4j
public class BpInvestigateTaskController {

    @Autowired
    private BpInvestigateTaskService taskService;

    @Autowired
    private BaseConfigService baseConfigService;

    @Autowired
    private WorkFlowProcessService processService;

    @Autowired
    private CreateTableHandler createTableHandler;


    @GetMapping("/record-history-list")
    @ApiOperation("检查记录列表")
    public ResultBean taskRecordHistoryList(TaskSearchBean search) {
        Page<TaskListVo> iPage = taskService.getTaskRecordHistoryList(search);
        return ResultBean.pageData(iPage.getRecords(), iPage.getTotal());
    }

    @GetMapping("/record-today-list")
    @ApiOperation("今日检查记录列表")
    public ResultBean taskRecordTodayList(TaskSearchBean search) {
        Page<TaskListVo> iPage = taskService.getTaskRecordTodayList(search);
        return ResultBean.pageData(iPage.getRecords(), iPage.getTotal());
    }


    @GetMapping("/record-history-new-list")
    @ApiOperation("检查记录列表")
    public ResultBean taskRecordHistoryNewList(TaskSearchBean search) {
        Page<TaskListVo> iPage = taskService.getTaskRecordHistoryNewList(search);
        return ResultBean.pageData(iPage.getRecords(), iPage.getTotal());
    }

    @GetMapping("/record-count-all")
    @ApiOperation("检查记录总数")
    public ResultBean getTaskRecordCountAll() {
        Long countAll = taskService.getTaskRecordCountAll();
        return ResultBean.success(countAll);
    }

    @GetMapping("/record-detail")
    @ApiOperation("检查记录详情")
    public ResultBean taskRecordDetail(Long taskId) {
        CheckRecordVo detail = taskService.getRecordDetail(taskId);
        return ResultBean.success(detail);
    }

    @GetMapping("delete-record")
    @ApiOperation("删除记录")
    public ResultBean deleteRecord(Long id) {
        return ResultBean.success(taskService.deleteRecord(id));
    }

    @GetMapping("statistics-list")
    @ApiOperation("统计报表列表页部分")
    public ResultBean getStatisticsList(TaskSearchBean search) {
        Page<TaskStatiUserVo> statistics = taskService.getStatistics(search);
        return ResultBean.pageData(statistics.getRecords(),statistics.getTotal());
    }

    @GetMapping("sum-troubleshooting")
    @ApiOperation("运行机制效果-各部门隐患排查完成情况")
    public ResultBean getBpTaskInvestigateBean(TaskSearchBean search) {
        BpTaskInvestigateBean bean = taskService.getBpTaskInvestigateBean(search);
        return ResultBean.success(bean);
    }

    @GetMapping("statistics-troubleshooting")
    @ApiOperation("运行机制效果-隐患排查完成率统计")
    public ResultBean troubleshootingStatistics(TaskSearchBean search) {
        Page<BpTaskInvestigateBean> statistics = taskService.getTroubleshootingStatistics(search);
        return ResultBean.pageData(statistics.getRecords(),statistics.getTotal());
    }



    @GetMapping("statistics-hidden-danger")
    @ApiOperation("运行机制效果-隐患治理完成情况")
    public ResultBean hiddenDangerStatistics(TaskSearchBean search) {
        Page<BpHiddenDangerBean> statistics = taskService.getBpHiddenDangerBeanStatistics(search);
        return ResultBean.pageData(statistics.getRecords(),statistics.getTotal());
    }
    @GetMapping("sum-hidden-danger")
    @ApiOperation("运行机制效果-隐患治理完成率")
    public ResultBean getBpHiddenDangerBean(TaskSearchBean search) {
        BpHiddenDangerBean bean = taskService.getBpHiddenDangerBean(search);
        return ResultBean.success(bean);
    }





    @GetMapping("statistics-graphical")
    @ApiOperation("统计报表图形部分")
    public ResultBean getStatisticsGraphical(TaskSearchBean search) {
        TaskStatiUserVo statistics = taskService.getStatisticsGraphical(search);
        return ResultBean.success(statistics);
    }

    @GetMapping("/export-statistics-list")
    @ApiOperation("统计报表列表页导出")
    public void exportStatisticsList(TaskSearchBean search, HttpServletRequest request, HttpServletResponse response) {
        try {
            taskService.exportStatisticsList(search, request, response);
        } catch (IOException e) {
            log.error("检查记录导出", e);
        }
    }


    @GetMapping("/export")
    @ApiOperation(value = "检查记录导出")
    public void exportRecord(TaskSearchBean search, HttpServletRequest request, HttpServletResponse
            response) {
        try {
            taskService.newExportReport(search, request, response);
        } catch (IOException e) {
            log.error("检查记录导出", e);
        }
    }

    @GetMapping("/move/data")
    @ApiModelProperty("手动调用迁移数据")
    @NoRepeatSubmit(seconds = 60)
    public ResultBean moveData(Date date) {
        taskService.moveDataHand(date);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/findTaskList")
    @ApiOperation("排查任务列表")
    public ResultBean findTaskList(TaskSearchVo search) {
        Page<TaskVo> iPage = taskService.findTaskList(search);
        return ResultBean.pageData(iPage.getRecords(),iPage.getTotal());
    }

    @PostMapping("/deleteBatch")
    @ApiModelProperty("批量删除任务")
    public ResultBean deleteBatch(@RequestBody TaskDelDTO bean){
        taskService.deleteBatch(bean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/delete/task")
    @ApiModelProperty("导入页面删除任务")
    public ResultBean deleteTask(@RequestBody TaskDelBean bean){
        String msg = taskService.deleteTask(bean);
        return ResultBean.success(msg);
    }

    @GetMapping("/processId-by-bizName")
    @ApiOperation("根据名称获取流程id")
    public ResultBean getWorkflowProcessIdByName() {
        BaseConfig baseConfig = baseConfigService.findConfig("workflow.process.name");
        WorkFlowProcess process = processService.findByBizName(baseConfig.getValue());
        return ResultBean.success(process != null ? process.getId() : null);
    }

    @GetMapping("/move/hisData")
    @ApiModelProperty("手动迁移历史数据")
    @NoRepeatSubmit(seconds = 60)
    public ResultBean moveHisData() {
        taskService.moveHisDataDaily();
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/createTable")
    @ApiModelProperty("手动创建隐患排查相关备份表")
    @NoRepeatSubmit(seconds = 60)
    public ResultBean moveData(String year) {
        if (StringUtil.isEmpty(year)) {
            return ResultBean.wrapErrorResultDefault("备份表年份必传!");
        }
        Map<String, String> map = new HashMap<>();
        map.put(TableNamePrefixConstant.INVESTIGATE_TASK, TableNamePrefixConstant.INVESTIGATE_TASK + "_" + year);
        map.put(TableNamePrefixConstant.INVESTIGATE_TASK_ASSIGN, TableNamePrefixConstant.INVESTIGATE_TASK_ASSIGN + "_" + year);
        map.put(TableNamePrefixConstant.INVESTIGATE_TASK_SUMMARY, TableNamePrefixConstant.INVESTIGATE_TASK_SUMMARY + "_" + year);
        map.put(TableNamePrefixConstant.INVESTIGATE_RECORD, TableNamePrefixConstant.INVESTIGATE_RECORD + "_" + year);
        map.put(TableNamePrefixConstant.INVESTIGATE_RECORD_ITEM, TableNamePrefixConstant.INVESTIGATE_RECORD_ITEM + "_" + year);
        createTableHandler.createTable(map);
        return ResultBean.defaultSuccessResult();
    }

}

